package com.example.jyjava.controller;

import com.alibaba.fastjson.JSON;
import com.example.jyjava.bean.Consent;
import com.example.jyjava.bean.Invn;
import com.example.jyjava.bean.ShopQueryInfo;
import com.example.jyjava.dao.ConsentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/consent")
public class ConsentController {

    @Autowired
    private ConsentDao consentDao;

    @RequestMapping("/addConsent")
    public String addConsent(@RequestBody Consent consent){
        String falg = "400";
        consent.setCreatTime(new Date());
        consent.setUpdateTime(new Date());
        consent.setDeleteType(false);
        consent.setLook(0);
        int i = consentDao.addConsent(consent);
        HashMap<String, Object> res = new HashMap<>();
        if (i > 0){
            falg = "200";
            res.put("flag",falg);
            res.put("consent",i);
        }else{
            res.put("flag",falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/getConsent")
    public String getConsent(ShopQueryInfo queryInfo){
        String falg = "400";
        int pageStart;
        int numbers;
        if (queryInfo.getPageSize() == -1){
            pageStart = 0;
            queryInfo.setPageSize(10000);
            numbers = consentDao.getConsentCounts("%" + queryInfo.getQuery() + "%",queryInfo.getUserId(),queryInfo.getConsentId());
        }else{
            numbers = consentDao.getConsentCounts("%" + queryInfo.getQuery() + "%",queryInfo.getUserId(),queryInfo.getConsentId());
            pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
        }
        List<Consent> consentList = consentDao.getConsent("%" + queryInfo.getQuery() + "%", pageStart, queryInfo.getPageSize(),queryInfo.getUserId(), queryInfo.getConsentId());
        HashMap<String, Object> res = new HashMap<>();
        if (consentList != null){
            falg = "200";
            res.put("flag",falg);
            res.put("consentList", consentList);
            res.put("numbers",numbers);
        }else{
            res.put("flag",falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }


    @RequestMapping("/getCDelete")
    public String getCDelete(@RequestBody Consent consent){
        String falg = "400";
        int i = consentDao.getCDelete(consent.getId());
        HashMap<String, Object> res = new HashMap<>();
        if (i > 0){
            falg = "200";
            res.put("flag",falg);
            res.put("consent",i);
        }else{
            res.put("flag",falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/getCon")
    @ResponseBody
    public String getCon(@RequestBody Consent consent) {
        String falg = "400";
//        System.out.println(id);
        Consent c = consentDao.getCon(consent.getId());
        HashMap<String, Object> res = new HashMap<>();  //map封装
        if (c != null) {
            falg = "200";
            res.put("flag", falg);
            res.put("consent", c);
        } else {
            res.put("flag", falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }
}
